package com.jushu.storbox.callback;

import java.util.Comparator;

import net.sourceforge.pinyin4j.PinyinHelper;
import android.annotation.SuppressLint;

import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.util.PingYinUtil;

public class FileFolderInfoNameSort implements Comparator<FileFolderInfo> {
	private int sortType ;
	public  FileFolderInfoNameSort(int sortType) {//0x0001 0x1001
		this.sortType = sortType;
	}
	@Override
	public int compare(FileFolderInfo lhs, FileFolderInfo rhs) {
		int compareAscResult = 1;
		
		String leftCode = PingYinUtil.convertFirstCharToString(lhs.getName());
		String rigthCode = PingYinUtil.convertFirstCharToString(rhs.getName());
		if(leftCode.equals(rigthCode)) {
			if(leftCode.equals(PingYinUtil.defaultStr) 
					&& rigthCode.equals(PingYinUtil.defaultStr)) {
				compareAscResult = compareFileFolderInfo(lhs, rhs);
				if(compareAscResult == 0) {
					compareAscResult = lhs.getName().compareTo(rhs.getName());
					return modifyResult(compareAscResult, true);
				}
				return modifyResult(compareAscResult, false);
			} else {
				compareAscResult = compareFileFolderInfo(lhs, rhs);
				if(compareAscResult == 0) {
					compareAscResult = getStringPinYin(lhs.getName())
						.compareTo(getStringPinYin(rhs.getName()));
					return modifyResult(compareAscResult, true);
				}
				return modifyResult(compareAscResult, false);
			}
		} else {
			if(leftCode.equals(PingYinUtil.defaultStr)) {
				compareAscResult = -1;
				return modifyResult(compareAscResult, false);
			} else if(rigthCode.equals(PingYinUtil.defaultStr)) {
				compareAscResult = 1;
				return modifyResult(compareAscResult, false);
			} else {
				compareAscResult = leftCode.compareTo(rigthCode);
				return modifyResult(compareAscResult, true);
			}
		}
	}
	
	private int modifyResult(int result, boolean modifyFlag) {
		if(!modifyFlag) {
			return result;
		} else {
			if(sortType == UiConstant.SORT_BY_NAME) {//升序排序
				return result;
			} else {
				return (result * -1);
			}
		}
	}
	
	private int compareFileFolderInfo(FileFolderInfo lhs, FileFolderInfo rhs) {
		if (lhs.getIsFile() == FileType.FOLDER_TYPE_VALUE 
				&& rhs.getIsFile() == FileType.FILE_TYPE_VALUE) {
			return -1;
		} else if (lhs.getIsFile() == FileType.FILE_TYPE_VALUE 
				&& rhs.getIsFile() == FileType.FOLDER_TYPE_VALUE) {
			return 1;
		} else {
			return 0;
		}
	}

	@SuppressLint("DefaultLocale")
	private String getStringPinYin(String name) {
		StringBuilder allPinyin = new StringBuilder();
		String tempPinyin = null;
		for (int i = 0; i < name.length(); ++i) {
			tempPinyin = getCharacterPinYin(name.charAt(i));
			if (tempPinyin == null) {
				// 如果str.charAt(i)非汉字，则保持原样
				allPinyin.append(name.charAt(i));
			} else {
				allPinyin.append(tempPinyin);
			}
		}
		return allPinyin.toString().toUpperCase();
	}

	private String getCharacterPinYin(char c) {
		String[] pinyin;
		pinyin = PinyinHelper.toHanyuPinyinStringArray(c);
		// 如果c不是汉字，toHanyuPinyinStringArray会返回null
		if (pinyin == null)
			return null;
		// 如果是多音字，仅取第一个发音
		return pinyin[0];
	}

}

